package com.ydd.common.hibernate3;

import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.rowset.SqlRowSet;

/**
 * JDBC DAO基类
 * 
 * 分页查询，拷贝更新等一些常用功能。
 */
public class JdbcBaseDao extends JdbcDaoSupport {

	
	
	public static String DOCCOUNT="3";       //主页问题滚动中     医生上线
	/**
	 * 获得查询数据库记录数
	 * 
	 * @return
	 */
	public int getRowCountHql(String sb) {
		String hql = sb;

//		int fromIndex = hql.toLowerCase().indexOf("from");
//
//		hql = hql.substring(fromIndex);
		String rowCountHql = hql.replace("fetch", "");

		int index = rowCountHql.toLowerCase().indexOf("order");
		if (index > 0) {
			rowCountHql = rowCountHql.substring(0, index);
		}
		
		int limit = rowCountHql.toLowerCase().indexOf("limit");
		if (limit > 0) {
			rowCountHql = rowCountHql.substring(0, limit);
		}
		
		hql = "select count(*) cnt  from (" + rowCountHql + ") as tmp";
		SqlRowSet set = getJdbcTemplate().queryForRowSet(hql);
		int ret = 0;
		while (set.next()) {
			ret = set.getInt("cnt");
		}
		return ret;
	}
	
	/**
	 * MYSQL特殊字符处理：LIKE
	 * 
	 * @return
	 */
	public String sqlQueryStringLike(String inStr) {
		String retStr;
		if (inStr == null) {
			return null;
		} else {
			retStr = inStr.replaceAll("'", "''");
			retStr = retStr.replaceAll("\\\\", "\\\\\\\\\\\\");
			retStr = retStr.replaceAll("%", "\\\\%");
			retStr = retStr.replaceAll("_", "\\\\_");
			return retStr;
		}
	}
	
	/**
	 * MYSQL特殊字符处理：JOIN
	 * 
	 * @return
	 */
	public String sqlQueryStringJoin(String inStr) {
		String retStr;
		if (inStr == null) {
			return null;
		} else {
			retStr = inStr.replaceAll("'", "''");
			retStr = retStr.replaceAll("\\\\", "\\\\\\\\");
			return retStr;
		}
	}
}
